#include "ObjectFactory.h"
#include <boost/thread/tss.hpp>

namespace phnq
{
	namespace phnqlets
	{
		static boost::thread_specific_ptr<ObjectFactory> currentObjectFactory;

		class DefaultObjectFactory : public ObjectFactory
		{
			private:
				UTHObject *obj;

			public:
				static DefaultObjectFactory* getInstance()
				{
					static DefaultObjectFactory instance;
					return &instance;
				}
			
				DefaultObjectFactory()
				{
					this->obj = new UTHObject();
				}
			
				UTHObject* createObject(UTHArray *args)
				{
					return obj;
				}
		};
	
		ObjectFactory* ObjectFactory::getCurrent()
		{
			ObjectFactory *current = currentObjectFactory.get();
			if(current)
				return current;
			else
				return DefaultObjectFactory::getInstance();
		}
	
		void ObjectFactory::setCurrent(ObjectFactory *factory)
		{
			currentObjectFactory.reset(factory);
		}
	
		void ObjectFactory::removeCurrent()
		{
			currentObjectFactory.release();
		}
	}
}
